package com.dd.cloud.common.annotate.validation.impl;

import com.dd.cloud.common.annotate.validation.Bank;
import com.dd.cloud.common.constants.RegularConstants;
import com.dd.cloud.common.utils.RegexUtils;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 银行卡号验证实现类
 *
 * @author zk
 */
public class BankValidation implements ConstraintValidator<Bank, Object> {
    private Bank bank;

    @Override
    public void initialize(Bank bankVal) {
        bank = bankVal;
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        String ph = (String) value;
        if (!bank.required() && StringUtils.isBlank(ph)) {
            return true;
        }
        if (bank.required() && StringUtils.isBlank(ph)) {
            return false;
        }
        return RegexUtils.regCheck(ph, RegularConstants.BANK_REGEXP);
    }
}
